import config from '@/config'
import storage from '@/utils/storage'
import constant from '@/utils/constant'
import {
	loginOut,
	getInfo,
	authLogin,
	login
} from '@/api/login'
import {
	getToken,
	setToken,
	removeToken
} from '@/utils/auth'



const user = {
	state: {
		token: getToken(),
		name: storage.get(constant.name),
		avatar: storage.get(constant.avatar),
		userId: storage.get(constant.userId),
		userInfo: storage.get(constant.userInfo),
	},

	mutations: {
		SET_TOKEN: (state, token) => {
			state.token = token
		},
		SET_USERINFO: (state, userInfo) => {
			state.userInfo = userInfo
			storage.set(constant.userInfo, userInfo)
		},
		CLEAN_INFO: (state) => {
			state.userInfo = {}
			storage.set(constant.userInfo, {})
		}
	},

	actions: {

		// 登录
		Login({
			commit
		}, userInfo) {
			const account = userInfo.account
			const password = userInfo.pwd
			return new Promise((resolve, reject) => {
				login(account, password).then(resp => {
					setToken(resp.token)
					commit('SET_TOKEN', resp.token)
					resolve()
				}).catch(error => {
					reject(error)
				})
			})
		},

		// 授权登录
		AuthLogin({
			commit
		}, loginForm) {
			return new Promise((resolve, reject) => {
				authLogin(loginForm).then(respones => {
					const token = respones.data.token
					if (token) {
						setToken(token)
						commit('SET_TOKEN', token)
					}
					resolve()
				}).catch(error => {
					reject(error)
				})
			})
		},



		SetUsreInfo({
					  commit
				  }, userInfo) {
			commit('SET_USERINFO', userInfo)
		},

		// 获取用户信息
		GetInfo({
			commit,
			state
		}) {
			return new Promise((resolve, reject) => {
				getInfo().then(resp => {

					// console.log('0003333000')
					// console.log(resp)
                    let a = '企业'
					// console.log('555555555')
					// console.log(resp.user.userEnterprises.filter(e=>{ return e.photo!=''}))

					resp.user?.userEnterprises.forEach(d=>{
						if(d.orgName == '物业'){
							a = '物业'
						}
					})
                    const roleData = {roleName:'',roleKey:'',roleId:''}
					if(resp.user?.roles?.length>1){
						resp.user?.roles.forEach(r=>{
							if(r.roleName.indexOf('警务室') != -1){
								roleData.roleName = r.roleName
								roleData.roleKey = r.roleKey
								roleData.roleId = r.roleId
							}
						})
					} else if(resp.user?.roles?.length==0){

					} else if(resp.user?.roles?.length==1){

						roleData.roleName = resp.user.roles[0].roleName
					}

					// resp.user?.roles.


					const user ={
						name:resp.user.name,
						deptId:resp.user.deptId,
						deptName:resp.user?.dept?.deptName,
						userEnterprises: a,
						newRoleName: roleData.roleName,
						newRoleKey: roleData.roleKey,
						newRoleId: roleData.roleId,
						userId: resp.user.userId,
						integral: resp.user.integral,
						userName: resp.user.userName,
						nickName: resp.user.nickName,
						openId: resp.user.openId,
						roleID:  resp.user.roleID,
						roleName:  resp.user.roleName,
						phonenumber: resp.user.phonenumber,
						avatar: resp.user?.userEnterprises[0]?.photo,
						entId:  resp.user.entID,
						entName:  resp.user.entName,
					}
					commit('SET_USERINFO', user)
					resolve({
						user
					})
				}).catch(error => {
					reject(error)
				})
			})
		},

		// 退出系统
		LoginOut({
			commit,
			state
		}) {
			return new Promise((resolve, reject) => {
				loginOut().then(() => {
					commit('SET_TOKEN', '')
					removeToken()
					storage.clean()
					commit('CLEAN_INFO')
					resolve()
				}).catch(error => {
					reject(error)
				})
			})
		}


	}
}

export default user